package com.ybfenpan.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.regex.Pattern;


public class StringUtil extends StringUtils {


	/**
	 * 校验手机号
	 *
	 * @param mobile
	 * @return 校验通过返回true，否则返回false
	 */
	public static boolean isMobile(String mobile) {
		return Pattern.matches("^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$", mobile);
	}

	/**
	 * 字符串转码ISO
	 * 
	 * @param str
	 *            str
	 * @return String
	 */
	public static String strToISO(String str) {
		if (str == null)
			return "";
		try {
			return new String(str.getBytes("GB2312"), "ISO8859-1");
		} catch (Exception e) {
			return str;
		}
	}

	/**
	 * 字符串转码gbk
	 * 
	 * @param str
	 * @return
	 */
	public static String strToGBK(String str) {
		if (str == null)
			return "";
		try {
			return new String(str.getBytes("ISO-8859-1"), "GBK");
		} catch (Exception e) {
			return str;
		}
	}

	/**
	 * @param str
	 * @return jsp: str = encodeURI(encodeURI(str))
	 */
	public static String decodeToUTF8(String str) {
		if (str == null)
			return "";
		try {
			return java.net.URLDecoder.decode(str, "UTF-8");
		} catch (Exception e) {
			return str;
		}
	}

	/**
	 * @param str
	 * @param splitRegx 用于str的分隔符
	 * @param unionRegx 用于形成新字符串的连接符
	 * @return
	 */
	public static String strArrToStr(String str, String splitRegx, String unionRegx) {
		if (null == str)
			return "''";
		String ret = "";
		String[] arr = str.split(splitRegx);
		for (int i = 0; i < arr.length; i++) {
			String t = arr[i];
			if (null == t || 0 == t.length())
				continue;
			ret += "'" + t.trim() + "'";
			if (i != arr.length - 1)
				ret += unionRegx;
		}
		return ret;
	}
	
	/**
     * 方法名称: split 内容摘要: 根据指定分隔符分隔字串,获得字串数组
     * @param inputValue 输入的字符串
     * @param regex 分隔符
     * @return String[] 返回的数据
     */
    public static String[] strToArray(String inputValue, String regex) {
        // 输入值是否为null
        if (inputValue == null) {
            inputValue = "";
        } // end if
        // 去掉两边空格
        inputValue = inputValue.trim();
        // 实例化StringTokenizer对象
        java.util.StringTokenizer sToken = new java.util.StringTokenizer(inputValue, regex);
        // 获取分隔个数
        int arraySize = sToken.countTokens();
        String[] ret = new String[arraySize];
        int index = 0;
        // 进行迭代
        while (sToken.hasMoreTokens()) {
            String token = sToken.nextToken().trim();
            ret[index] = token;
            index++;
        } // end while
        sToken = null;
        return ret;
    }
    
    /**
     * 把数组中数据转换为字符串用逗号分开.
     * @param arr arr
     * @param regex 连接符
     * @return String
     */
    public static String arrayToStr(Object[] arr, String regex) {
        String retStr = "";
        if (null != arr && 0 < arr.length) {
            int size = arr.length;
            for (int i = 0; i < size; i++) {
                retStr += arr[i];
                if (i + 1 != size) {
                    retStr += regex;
                }
            }
        }
        return retStr;
    }

	public static boolean arrContains(String[] arr, Object o){
		if(arr==null || 0==arr.length){
			return false;
		}
		for(String a : arr){
			if(a.trim().equals(o.toString())){
				return true;
			}
		}
		return false;
	}

	/**
	 * @Title: getDbStr
	 * @param obj
	 * @return String
	 * @author: Qiyh 2014-6-6
	 */
	public static String getDbStr(Object obj) {
		String retStr = "";
		try {
			if (null != obj) {
				if (obj instanceof java.sql.Clob) {
					Reader reader = ((java.sql.Clob) obj).getCharacterStream();
					BufferedReader br = new BufferedReader(reader, 8192);
					char[] cbuf = new char[8192];
					int j = 0;
					StringBuffer bf = new StringBuffer();
					while ((j = br.read(cbuf)) != -1) {
						bf.append(cbuf, 0, j);
					}
					retStr = bf.toString();
				} else {
					retStr = String.valueOf(obj);
				}
			}
		} catch (Exception ex) {
			System.out.println("CLOB转String发生异常!");
			ex.printStackTrace();
		}
		return retStr;
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public static String getStrByCharcode(int code) {
        return String.valueOf((char)code);
    }
	
	/** 
     * 将驼峰字段转成属性字符串<br> 
     * (例:branch_no -> branchNo )<br> 
     *  
     * @param inputString 
     *            输入字符串 
     * @return 
     */  
    public static String toCamelCase(String inputString) {  
        return toCamelCase(inputString, false);  
    }  
  
    /** 
     * 将驼峰字段转成属性字符串<br> 
     * (例:branch_no -> branchNo )<br> 
     * @param inputString 
     *            输入字符串 
     * @param firstCharacterUppercase 
     *            是否首字母大写 
     * @return 
     */  
	public static String toCamelCase(String inputString, boolean firstCharacterUppercase) {
		if (inputString == null)
			return null;
		StringBuilder sb = new StringBuilder();
		boolean nextUpperCase = false;
		for (int i = 0; i < inputString.length(); i++) {
			char c = inputString.charAt(i);
			switch (c) {
			case '_':
			case '-':
			case '@':
			case '$':
			case '#':
			case ' ':
			case '/':
			case '&':
				if (sb.length() > 0) {
					nextUpperCase = true;
				}
				break;
			default:
				if (nextUpperCase) {
					sb.append(Character.toUpperCase(c));
					nextUpperCase = false;
				} else {
					sb.append(Character.toLowerCase(c));
				}
				break;
			}
		}
		if (firstCharacterUppercase) {
			sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		}
		return sb.toString();
	}
	
	/** 
     * 将属性字符串转成驼峰字段<br> 
     * (例:branch_no -> branchNo )<br> 
     * @param inputString 
     *            输入字符串 
     * @param separator 
     *            连接符
     * @return 
     */  
	public static String toUnderlineName(String inputString, String separator) {
        if (inputString == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < inputString.length(); i++) {
            char c = inputString.charAt(i);
            boolean nextUpperCase = true;
            if (i < (inputString.length() - 1)) {
                nextUpperCase = Character.isUpperCase(inputString.charAt(i + 1));
            }
            if ((i >= 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    if (i > 0) sb.append(separator);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }
            sb.append(Character.toLowerCase(c));
        }
        return sb.toString();
    }
	
	//首字母转小写     
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
	} 
	// 首字母转大写 
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	public static String htmlLineChar(String cont){
		if(null != cont){
			cont = cont.trim().replaceAll("\r\n", "\n")
//					.replaceAll("&", "&amp;")
//					.replaceAll("<", "&lt;")
//					.replaceAll(">", "&gt;")
//					.replaceAll("'", "&apos;")
//					.replaceAll("\"", "&quot;")
					.replaceAll("\"", "")
					.replaceAll("\n", "<br>");
		}
		return cont;
	}

	public static String toChineseNum(int n){
		String nStr = String.valueOf(n);
		if(nStr.length() == 1) {
			if (n == 0) {
				return "零";
			} else if (n == 1) {
				return "一";
			} else if (n == 2) {
				return "二";
			} else if (n == 3) {
				return "三";
			} else if (n == 4) {
				return "四";
			} else if (n == 5) {
				return "五";
			} else if (n == 6) {
				return "六";
			} else if (n == 7) {
				return "七";
			} else if (n == 8) {
				return "八";
			} else if (n == 9) {
				return "九";
			} else if (n == 10) {
				return "十";
			}
		}else if(nStr.length() == 2){
			return toChineseNum(nStr.charAt(0))+"十"+toChineseNum(nStr.charAt(1));
		}
		return "";
	}
	
}
